﻿using RedirectManager.Interfaces;
using RedirectManager.Pipelines.HttpRequest;
using Sitecore;
using Sitecore.Configuration;
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Diagnostics;
using Sitecore.Globalization;
using Sitecore.Web.UI.Sheer;
using System;
using System.Web;

namespace RedirectManager.Shell.Framework.Pipelines
{
    [Serializable]
    public class DeleteRedirect
    {
        private ILookupProvider provider;

        public DeleteRedirect()
        {
            provider = Redirector.Provider;
        }

        public void CheckPermissions(ClientPipelineArgs args)
        {
            Assert.ArgumentNotNull(args, "args");
            if (SheerResponse.CheckModified())
            {
                string str = args.Parameters["id"];
                Item item = Sitecore.Context.Database.GetItem(str);
                if (item != null)
                {
                    if (!item.Access.CanWrite())
                    {
                        // Currently riding on the user's permission to write to the item to determine wether the user can create a redirect to it.
                        // TODO: Create new role for redirect permissions
                        SheerResponse.Alert(Translate.Text("You do not have permission to delete redirect item \"{0}\".", new object[] { item.DisplayName }), new string[0]);
                        args.AbortPipeline();
                    }
                }
                else
                {
                    SheerResponse.Alert("Item not found.", new string[0]);
                    args.AbortPipeline();
                }
            }
        }

        public void Delete(ClientPipelineArgs args)
        {
            if (!args.IsPostBack)
            {
                // Show confirmation
                Context.ClientPage.ClientResponse.Confirm(string.Format("Delete redirect {0} ?", HttpUtility.UrlDecode(args.Parameters["requestPath"])));
                args.WaitForPostBack();
            }
            else if (args.HasResult)
            {
                // Delete redirect?
                if (args.Result.Equals("yes", StringComparison.OrdinalIgnoreCase))
                {
                    // Delete command confirmed by user
                    this.provider.Delete(HttpUtility.UrlDecode(args.Parameters["requestPath"]));

                    // Create confirmation prompt
                    Context.ClientPage.ClientResponse.Alert("Deleted");

                    // Refresh current item
                    var refresh = string.Format("item:load(id={0})", args.Parameters["targetId"]);
                    Sitecore.Context.ClientPage.ClientResponse.Timer(refresh, 2);
                }
                else
                {
                    // Cancel command 
                    Context.ClientPage.ClientResponse.Alert("Cancelled");
                    args.AbortPipeline();
                }
            }
            else
            {
                // No result
                args.AbortPipeline();
            }
        }


    }
}
